<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="被监测设备" prop="monitorDeviceId">
        <el-select
          v-model="queryParams.monitorDeviceId"
          clearable
          class="!w-240px"
          placeholder="请选择被监测设备"
        >
          <el-option
            v-for="item in monitorDeviceList"
            :key="item.deviceId"
            :label="item.deviceName"
            :value="item.deviceId"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="operate-bar">
      <el-button type="primary" plain @click="openForm()" v-hasPermi="['vmiot:bigscreen:create']">
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
    </div>
    <el-empty v-if="!list.length" description="暂无数据" />
    <el-row v-if="list.length" :gutter="20">
      <el-col v-for="item in list" :key="item.id" :span="6">
        <div class="screen-item">
          <img class="screen-img" :src="!item.cover ? defaultImg : item.cover" alt="" />
          <div class="screen-info">
            <div class="name">
              <el-tooltip effect="dark" :content="item.name" placement="top">
                {{ item.name }}
              </el-tooltip>
            </div>
            <div class="operation">
              <el-tooltip effect="dark" content="预览" placement="top">
                <el-icon color="#fff" :size="20" @click="viewDesign(item.id)"><View /></el-icon>
              </el-tooltip>
              <el-tooltip effect="dark" content="分享" placement="top">
                <el-icon color="#fff" :size="20" @click="shareDesign(item.id)"><Share /></el-icon>
              </el-tooltip>
              <el-tooltip effect="dark" content="编辑" placement="top">
                <el-icon color="#fff" :size="20" @click="openForm(item.id)"><Edit /></el-icon>
              </el-tooltip>

              <el-tooltip effect="dark" content="复制" placement="top">
                <el-icon color="#fff" :size="20" @click="handleCopy(item.id)"
                  ><CopyDocument
                /></el-icon>
              </el-tooltip>

              <el-tooltip effect="dark" content="分配租户" placement="top">
                <el-icon
                  color="#fff"
                  :size="20"
                  v-hasRole="['super_admin']"
                  @click="openTenantChangeForm(item)"
                  ><User
                /></el-icon>
              </el-tooltip>

              <el-tooltip effect="dark" content="删除" placement="top">
                <el-icon color="#fff" :size="20" @click="handleDelete(item.id)"><Delete /></el-icon>
              </el-tooltip>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <BigscreenTenantChangeForm ref="changeTenantFormRef" @success="getList" />
  </ContentWrap>
</template>

<script setup lang="ts">
import defaultImg from '@/assets/images/charts.jpg'
import { Edit, View, Share, Delete, User, CopyDocument } from '@element-plus/icons-vue'
import { BigscreenApi } from '@/api/vmiot/bigscreen'
const router = useRouter()
import BigscreenTenantChangeForm from './BigscreenTenantChangeForm.vue'

/** 任务 列表 */
defineOptions({ name: 'Task' })

const { push } = useRouter()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const monitorDeviceList = ref<any[]>([])
const loading = ref(true) // 列表的加载中
const list = ref<any[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  state: undefined,
  monitorDeviceId: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BigscreenApi.getBigscreenPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const changeTenantFormRef = ref()
const openTenantChangeForm = (rowData: any) => {
  changeTenantFormRef.value.open(rowData)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  if (id) {
    push('/bigscreen/designer?id=' + id)
  } else {
    push('/bigscreen/designer')
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()

    // 发起删除
    await BigscreenApi.deleteBigscreen(id)

    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 复制按钮操作 */
const handleCopy = async (id: number) => {
  try {
    await ElMessageBox.confirm('确定要复制该大屏吗？', '复制大屏', {
      confirmButtonText: t('common.ok'),
      cancelButtonText: t('common.cancel'),
      type: 'warning'
    })
    await BigscreenApi.copy(id)
    message.success('复制成功')
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    // const data = await TaskApi.exportTask(queryParams)
    // download.excel(data, '任务.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const viewDesign = (val) => {
  const routeUrl = router.resolve({
    path: '/bigscreen/viewer',
    query: { id: val }
  })
  window.open(routeUrl.href, '_blank')
}

const shareDesign = (val) => {
  const routeUrl = router.resolve({
    path: '/bigscreen/share',
    query: { id: val }
  })
  window.open(routeUrl.href, '_blank')
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.screen-item {
  width: 100%;
  height: 220px;
  position: relative;
  overflow: hidden;
  margin: 10px 0;
  border: 12px solid white;
}

.screen-item .screen-img {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 2;
}
.screen-item .screen-info {
  position: absolute;
  bottom: 0;
  position: absolute;
  left: 0;
  height: 32px;
  width: 100%;
  z-index: 3;
  display: flex;
  align-items: center;
  padding: 0 10px;
  background-color: rgba(0, 0, 0, 0.6);

  .name {
    color: #fff;
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .operation {
    display: flex;
    justify-content: space-between;

    .el-icon {
      margin-left: 10px;
      cursor: pointer;
    }
  }
}
</style>
